﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    [CustomEditor(typeof(BuildInShaderImportRule), true)]
    public class BuildInShaderImportRuleEditor : Editor
    {
        private int startindex = -1;
        private Vector2 pos;

        private SerializedProperty replace_Standerd, replace_ParticleStanderdSurface, Default_Shader_Path;

        private void OnEnable()
        {
            if (this.target == null)
            {
                return;
            }
            SerializedObject serializedObject = base.serializedObject;
            this.replace_Standerd = serializedObject.FindProperty("replace_Standerd");
            this.replace_ParticleStanderdSurface = serializedObject.FindProperty("replace_ParticleStanderdSurface");
            Default_Shader_Path = serializedObject.FindProperty("Default_Shader_Path");
        }


        public override void OnInspectorGUI()
        {
            serializedObject.Update();
            EditorGUILayout.HelpBox("\n此规则文件用于使用本地优化的Standard Shader替换Material的内置Shader，降低内存和Shader.Parse性能消耗。\n" +
                "注意：\n" +
                "    需要替换的标准Standard Shader到Unity官网上下载到本地，优化屏蔽变体。\n", MessageType.Info);
            EditorGUILayout.Space();

            GUILayout.Label("匹配规则", GUITheme.LabelTitleStyle);
            GUILayoutEx.BeginContents();
            GUI.color = Color.yellow;
            EditorGUILayout.HelpBox("注意：规则文件只会检测同一父目录或同一级目录下的资源。\n", MessageType.None);
            GUI.color = Color.white;
            GUILayoutEx.EndContents();
            EditorGUILayout.Space();

            //EditorGUILayout.LabelField(new GUIContent("Default Buildin Shader Path:"), new GUIContent(this.Default_Shader_Path.stringValue));
            GUI.enabled = false;
            EditorGUILayout.PropertyField(this.Default_Shader_Path, new GUIContent("Default Buildin Shader Path:"));
            GUI.enabled = true;
            EditorGUILayout.Space();

            EditorGUILayout.PropertyField(this.replace_Standerd, new GUIContent("Replace Standard Shader"));
            EditorGUILayout.PropertyField(this.replace_ParticleStanderdSurface, new GUIContent("Replace Particle Standard Surface"));

            EditorGUILayout.Space();
            EditorGUILayout.BeginHorizontal(GUITheme.ListItemHeaderStyle);
            if (GUILayoutEx.ActionButton("检测Material的内置Shader"))
            {
                BuildInShaderImportRule rule = this.target as BuildInShaderImportRule;
                rule.FixMaterialsStandardShader(false);
            }
            if (GUILayoutEx.ActionButton("替换Material的内置Shader"))
            {
                BuildInShaderImportRule rule = this.target as BuildInShaderImportRule;
                rule.FixMaterialsStandardShader(true);
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();

            if (GUILayoutEx.ActionButton("查找替换了内置Shader的Material"))
            {
                BuildInShaderImportRule rule = this.target as BuildInShaderImportRule;
                rule.FindMaterialsStandardShader();
            }

            BuildInShaderImportRule rule1 = this.target as BuildInShaderImportRule;
            var list = rule1.Report.Reports;
            if (list.Count > 0)
            {
                EditorGUILayout.PrefixLabel(string.Format("Report({0}):  (鼠标滚轮滑动)", list.Count));
                AssetCheckReportItem.Show(list, ref this.pos, ref this.startindex);
            }

            serializedObject.ApplyModifiedProperties();
        }
    }
}

